<?php

/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 16/4/5
 * Time: 上午9:43
 */
class NoticeController extends AdminBaseController {
    
    public function index(){
    	$this->setTitle('邮件公告列表');
    
    	$filter = new PubFilter();
    	$grid   = new PubGrid();
    
//     	$filter->addSeaOsFilter();
//     	$filter->addOssServer(true)
    	$filter->addSeaSpecialFilter(true);
    	$filter->addSeaOsFilterAll();
    	$filter->addOssServerAll()
    	->addFilter('begin', 'input', '创建时间', array('date' => 'day'))
    	->addFilter('end', 'input', '至', array('date' => 'day'));
    
    	$params = $filter->get();
    	$header = NoticeService::indexHeader();
    	$data   = NoticeService::indexData($params);
    
    	$grid->addGrid($header, $data);
    
    	$this->out['filter'] = $filter->show();
    	$this->out['grid'] = $grid->show();
    }



    public function add(){
        $this->setTitle('添加邮件公告');

        $this->out['serverList']    = ServerService::getOptions();
        $this->out['sendType']      = NoticeService::sendType();

    }

    public function doAdd(){
    	$result = NoticeService::doAdd($_POST);
    
    	if($result['state']){
    		$this->showMessage('添加成功', PubFunc::buildUrl('/oss/?r=notice/index'));
    	}else{
    		$this->showMessage($result['msg']);
    	}
    }
    
    public function delete(){
    	$id = $this->R('id');
    
    	$result = NoticeService::delete($id);
    
    	if($result !== false){
    		$this->showMessage('删除成功', PubFunc::buildUrl("/oss/?r=notice/index"));
    	}else{
    		$this->showMessage('删除失败');
    	}
    }
    
    public function synchro(){
    	$ids = PubRequest::R('ids');
    	if(PubFunc::isAjax()){
    		$servers = PubRequest::R('servers');
    		$result = NoticeService::synchro($ids,$servers);
    		$this->out = array(
    				'status' => 1,
    				'msg' => $result,
    				'data'    => ''
    		);
    		$this->outType = 'json';
    	}else{
    
    		$list = PubServer::getChannelServer();
    
    		$this->out['list'] = $list;
    		$this->out['ids'] = $ids;
    		$this->layout = '';
    
    		$url = PubFunc::buildUrl("/oss/?r=notice/synchro");
    		$this->out['url'] = $url;
    
    
    	}
    }

}